﻿using IceDog.UpdateDB.Model.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;

namespace IceDog.UpdateDB.WebServer
{
    /// <summary>
    /// 用于EF Tool 的Context构建类
    /// 参考：https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dbcontext-creation
    /// </summary>
    public class EFContextFactory : IDesignTimeDbContextFactory<EFContext>
    {
        public EFContext CreateDbContext(string[] args)
        {
            var text = File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "appsettings.json"));
            var config = JsonConvert.DeserializeObject(text) as JObject;
            var connectionString = config["ConnectionStrings"]["SqliteConn"].ToString();
            var optionsBuilder = new DbContextOptionsBuilder<EFContext>();
            optionsBuilder.UseSqlite(connectionString);
            return new EFContext(optionsBuilder.Options);
        }
    }
}
